<?php
    /**
     * Gets the last saved order by user id
     * Return order_m_order object if there is one else returns NULL
     * @return object|NULL
     */
    function get_last_saved_user_order($user_id){
        $order_obj = class_loader::new_obj("order_m_order");
        $order_obj->where('status',  status_m_order::SAVED)->get_by_user_id($user_id);
        return $order_obj->exists() ? $order_obj : NULL;
    }
    /**
     * Gets the last saved order by guest user session id
     * Return order_m_order object if there is one else returns NULL
     * @return object|NULL
     */
    function get_last_saved_guest_order($session_id){
        $order_obj = class_loader::new_obj("order_m_order");
        $order_obj->where('status',  status_m_order::SAVED)->get_by_guest_session_id($session_id);
        return $order_obj->exists() ? $order_obj : NULL;
    }

    /**
     * Gets the order id of the current/saved order
     * @return int|NULL
     */
    function get_order_id(){
        $order_id = NULL;
        $order = get_order_object();
        if($order!==NULL){
            $order_id = $order->id;
        }
        return $order_id;

    }

     /**
     * Gets the order id of the current/saved order
     * @return object|NULL
     */
    function get_order_object(){
        $order = NULL;
        if(is_user_logged_in()){
            $user =  get_logged_user();
            $last_saved = get_last_saved_user_order($user->user_id);
            if($last_saved!==NULL){
                $order =  $last_saved;
            }
        }else{
            $session_id = session_id();
            $session_order = get_last_saved_guest_order($session_id);
             if($session_order!==NULL){
                $order = $session_order;
            }
        }
        return $order;
    }

?>
